//
//  BETableToCollectionCell.m
//  s.
//
//  Created by fanshijian on 2022/8/25.
//  Copyright © 2022 J. All rights reserved.
//

#import "BETableToCollectionCell.h"
#import "BETableToCollectionModel.h"
#import "BESimpleCollectionViewAdapter.h"

@interface BETableToCollectionCell ()
@property (strong, nonatomic) UICollectionView *collectionView;
@end


@implementation BETableToCollectionCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView setBackgroundColor:[UIColor clearColor]];
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        // cell的行边距[ = ](上下边距)
        layout.minimumLineSpacing = 0.0f;
        // cell的纵边距[ || ](左右边距)
        layout.minimumInteritemSpacing = 0.0f;
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        UICollectionView *obj = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        [self.contentView addSubview:_collectionView = obj];
        BETableToCollectionModel *model = self.model;
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            kMakeEdgeV(model.collectionInsets);
        }];
        obj.showsHorizontalScrollIndicator = NO;
        [obj setBackgroundColor:[UIColor clearColor]];
        if (@available(iOS 11.0, *)) {
            obj.contentInsetAdjustmentBehavior     = UIScrollViewContentInsetAdjustmentNever;
        }
        obj.collectionViewAdapter = [BESimpleCollectionViewAdapter new];
    }
    return _collectionView;
}

- (void)setModel:(BETableToCollectionModel *)model {
    [super setModel:model];
    
    self.collectionView.collectionViewAdapter.keyPathOfSubData = @"items";
    self.collectionView.headerDatas = model.items;
    self.collectionView.cellDatas = model.items;
    self.collectionView.footerDatas = model.items;
    [self.collectionView reloadData];
}

@end
